package com.shopping.mall.core.module.freight.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.freight.entity.Freight;
import com.shopping.mall.core.module.freight.service.FreightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "运费")
@RestController
@RequestMapping("/freight")
public class FreightController {
    @Autowired
    private FreightService freightService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Freight>> getFreightPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Freight freight) {
        IPage<Freight> freightPage=freightService.getFreightPage(current,size,freight);
        return Result.OK(freightPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Freight> update(@Validated(Freight.update.class) @RequestBody Freight freight) {
        freightService.updateById(freight);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Freight.insert.class) @RequestBody Freight freight) {
        try {
            freightService.save(freight);
            return Result.OK();
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            return Result.error(ResponseState.DATA_IS_EXIST.getValue(), ResponseState.DATA_IS_EXIST.getMessage());
        }
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Freight> getById(@RequestParam String id){
        Freight freight=freightService.getById(id);
        return Result.OK(freight);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Freight>> getAll(Freight freight){
        List<Freight> freightList=freightService.getAll(freight);
        return Result.OK(freightList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return freightService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return freightService.batchDelete(ids);
    }


}
